package com.xsherl.leetcode.solution;

import cn.hutool.core.lang.Assert;

public class RemoveElement {

    /**
     * 双指针, m 是值为val的子数组最左侧下标，i为遍历数组的下标
     * 当 nums[i]的值不等于val时，和nums[m]交换，保持m左侧元素均不等于val值，
     *
     * @param nums
     * 0 <= nums.length <= 100
     * 0 <= nums[i] <= 50
     *
     * @param val
     * 0 <= val <= 100
     */
    public int removeElement(int[] nums, int val) {
        int m = 0;
        for (int i = 0; i < nums.length; ++i){
            if (nums[i] != val){
                int t = nums[i];
                nums[i] = nums[m];
                nums[m] = t;
                m++;
            }
        }
        return m;
    }

    public static void main(String[] args) {
        int[] nums = {3};
        int val = 3;
        int i = new RemoveElement().removeElement(nums, val);
        Assert.isTrue(i == 2);
    }

}
